<template>
  <div class="WorkingSchedule" v-loading="loading">
    <el-form ref="form" :model="form" label-position="top" class="myform">
      <div>
        <el-form-item label="批次标题" class="myform-item">
          <el-input
            v-model="form.batch_title"
            placeholder="请输入批次标题"
            clearable
            @keyup.enter.native="onSearch"
          ></el-input>
        </el-form-item>
        <el-form-item label="批次日期" class="myform-item">
          <el-date-picker
            value-format="yyyy-MM-dd"
            v-model="form.batch_date"
            type="date"
            placeholder="选择日期"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item style="margin-top: 50px">
          <el-button type="primary" @click="onSearch">查询</el-button>
        </el-form-item>
      </div>
      <div>
        <el-form-item style="margin-top: 50px">
          <el-button type="primary" @click="onEdit()" icon="el-icon-plus"
            >新建</el-button
          >
        </el-form-item>
      </div>
    </el-form>
    <el-table :data="tableData" style="width: 100%" border>
      <el-table-column
        label="批次标题"
        align="center"
        prop="batch_title"
    ></el-table-column>
        <el-table-column
            label="批次日期"
            align="center"
            prop="batch_date"
        ></el-table-column>
        <el-table-column label="批次状态" align="center" prop="status">
            <template slot-scope="{ row }">
                <div :style="{color:row.status == 0 ?'red':'#67c23a'}">
                    {{ row.status == 0 ?'禁用':'启用' }}
                </div>
            </template>
        </el-table-column>
      <el-table-column label="操作" align="center" fixed="right">
        <template slot-scope="{ row }">
          <el-button type="text" @click="onEdit(row)">编辑</el-button>
          <!-- <el-button type="text" @click="onLook(row.type_no)">查看</el-button> -->
          <el-button
            type="text"
            style="color: red"
            v-if="row.status == 1"
            @click="onIs(row.id, 0)"
            >禁用</el-button
          >
          <el-button
            type="text"
            style="color: #67c23a"
            v-if="row.status == 0"
            @click="onIs(row.id, 1)"
            >启用</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <pagination
      :form="form"
      :total="total"
      @paginationPostOrderTaskList="postAdminOperateGetbatch"
    />
    <el-dialog
      title="批次"
      :visible.sync="dialogVisible"
      width="30%"
      :before-close="handleClose"
    >
      <div>
        <el-form ref="form" :model="data" label-width="100px">
          <el-form-item label="批次标题" class="myform-item" prop="batch_title">
            <el-input
              style="width: 220px"
              v-model="data.batch_title"
              placeholder="请输入批次标题"
              clearable
            ></el-input>
          </el-form-item>
          <el-form-item label="批次日期" class="myform-item" prop="batch_date">
            <el-date-picker
              value-format="yyyy-MM-dd"
              v-model="data.batch_date"
              type="date"
              placeholder="选择日期"
            >
            </el-date-picker>
          </el-form-item>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button
          type="primary"
          :disabled="!data.batch_title || !data.batch_date"
          @click="onClickSubmit"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

  <script>
import {
  postAdminOperateGetbatch,
  postAdminOperateSetbatch,
  postAdminOperateDelOrtherProduct,
  postAdminOperateDelbatch,
} from "@/api";
import pagination from "@/components/pagination.vue";
export default {
  components: { pagination },
  data() {
    return {
      loading: false,
      dialogVisible: false, //审核
      qxobj: "",
      data: {
        batch_title: "",
        batch_date: "",
      },
      form: {
        batch_title: "",
        batch_date: "",
        page: 1,
        size: 10,
      },
      tableData: [],
      total: 1,
    };
  },
  activated() {
    //   this.qxobj = JSON.parse(this.$route.query.qxobj);
    let form = this.form;
    this.postAdminOperateGetbatch(form);
  },
  methods: {
    handleClose() {},
    // 查看
    onEdit(row) {
      if (row) {
        this.data = {
          id: row.id,
          batch_title: row.batch_title,
          batch_date: row.batch_date,
        };
      } else {
        this.data = {
          batch_title: "",
          batch_date: "",
        };
      }

      this.dialogVisible = true;
    },
    // 保存
    async onClickSubmit() {
      this.loading = true;
      let { data } = this;
      try {
        await postAdminOperateSetbatch(data);
        let form = this.form;
        this.postAdminOperateGetbatch(form);
        this.dialogVisible = false;
        this.loading = false;
      } catch (error) {
        this.loading = false;
      }
    },
    onLook(type_no) {
      if (!type_no) {
        this.$router.push({
          name: "processPDF",
        });
      } else {
        this.$router.push({
          name: "processPDF",
          query: { type_no },
        });
      }
    },
    async onIs(id, status) {
      let obj = {
        id,
        status,
      };
      this.$confirm(`确定${status == 0 ? "禁用" : "启用"}`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          await postAdminOperateDelbatch(obj);
          let form = this.form;
          this.postAdminOperateGetbatch(form);
        })
        .catch(() => {});
    },
    // 删除
    onDelete(id) {
      let obj = {
        id,
      };
      let { form } = this;
      this.$confirm("确定删除该标签种类?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          postAdminOperateDelOrtherProduct(obj).then((res) => {
            this.postAdminOperateGetbatch(form);
            this.$message({
              type: "success",
              message: "删除成功!",
            });
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },

    // 采购列表
    async postAdminOperateGetbatch(data) {
      this.loading = true;
      this.form = data;
      postAdminOperateGetbatch(data)
        .then((res) => {
          this.loading = false;
          console.log("采购列表", res);
          this.tableData = res.data;
          this.total = Number(res.total);
        })
        .catch((err) => {
          this.loading = false;
        });
    },
    // 搜索
    onSearch() {
      this.form.page = 1;
      this.form.size = 10;
      this.postAdminOperateGetbatch(this.form);
    },
  },
};
</script>

  <style lang="scss" scoped>
.myform {
  display: flex;
  justify-content: space-between;
  > div {
    display: flex;
  }
  &-item {
    margin-right: 10px;
  }
}
.WorkingSchedule {
  background: #ffff;
  padding: 20px;
  border-radius: 2px;
  @include boxShadow;
}
.goods {
  display: flex;
  align-items: center;
  justify-content: center;
  .goods-img {
    width: 60px;
    height: 60px;
    margin-right: 10px;
  }
  .goods-div {
    width: 300px;
    > div:nth-child(2) {
      color: #999;
    }
  }
}
</style>
